//
//  netcon.h
//  GCloud Sync
//
//  Created by Alex on 9/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#ifndef netcon_h
#define netcon_h


#include "lib-netio.h"


#include <netdb.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <pthread.h>
#include <unistd.h>

typedef struct _nconn nconn;

typedef struct _ncallbacks {
    void (* open)(nconn *);
    void (* read)(nconn *);
    void (* error)(nconn *);
    void (* close)(nconn *);
} ncallbacks;

struct _nconn {
    char * host;
    u_int16_t port;
    u_int16_t timeout;
    
    int socket;
    struct hostent * server;
    struct sockaddr_in serv_addr;
    int conn;
    
    bool open;
    struct _ncallbacks cb;
    pthread_t pthread;
    
    int error;
};


nconn * nopen(const char * host, u_int16_t port, u_int16_t timeout, ncallbacks cb);

ssize_t nwrite(nconn * conn, const char * data, size_t len);
ssize_t nread(nconn * conn, void * buf, size_t len);
ssize_t npeek(nconn * conn, void * buf, size_t len);

void nclose(nconn * conn);

void nwait(nconn * conn);

#endif
